package main

import (
	"fmt"
	"go-the-way-of-learning/day9/4tail_log_project/conf"
	"go-the-way-of-learning/day9/4tail_log_project/kafka"
	"go-the-way-of-learning/day9/4tail_log_project/taillog"
	"gopkg.in/ini.v1"
	"time"
)

// 加载配置文件
var cfg = new(conf.AppConf)

func run() {
	// 1、 读日志文件
	for true {
		select {
		case line := <-taillog.ReadLogChan():
			// 2、 发送kafka
			kafka.SendKafka(cfg.KafkaConf.Topic, line.Text)
		default:
			time.Sleep(time.Second)
		}
	}
}

func main() {
	err := ini.MapTo(cfg, "./day10//conf/config.ini")
	if err != nil {
		fmt.Printf("load ini failed, err: %v\n", err)
		return
	}
	// 初始化kafka连接
	err = kafka.Init([]string{cfg.KafkaConf.Address})
	if err != nil {
		fmt.Printf("init kafka failed, err: %T\n", err)
		return
	}
	fmt.Println("kafka init success")

	// 打开日志文件准备收集日志
	err = taillog.Init(cfg.TaillogConf.FileName)
	if err != nil {
		fmt.Printf("Init taillog failed, err: %T\n", err)
		return
	}
	fmt.Println("taillog init success")

	run()
}
